﻿using Sasc.PV.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Management;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sasc.PV.Web
{
    public class WebUtils
    {
        #region Static ReadOnly Fields
        public static readonly string AllowedIP = ConfigurationManager.AppSettings["OAIP"];
        public static readonly string OALoginType = ConfigurationManager.AppSettings["OALogin"].Trim();
        public static readonly string SystemName = ConfigurationManager.AppSettings["SYSTitle"];
        public static readonly string CompanyName = ConfigurationManager.AppSettings["CompanyName"];

        public static readonly string LoginPageURL = "~/Account/Login";
        public static readonly string AdminPageURL = "~/Admin/Index";
        public static readonly string ActivatePageURL = "~/Activate";
        public static readonly string DefaultPageURL = "~/Default";
        #endregion

        #region MakeRelative
        public static string MakeRelative(string toPath, string fromPath = null)
        {
            fromPath = fromPath ?? HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;
            return VirtualPathUtility.MakeRelative(fromPath, toPath);
        }
        #endregion

        #region Check Session
        //判断Session是否有效
        public static bool CheckSession()
        {
            //测试的时候注释掉
            try
            {
                if (System.Web.HttpContext.Current.Session["UserName"] == null)
                {
                    FormsAuthentication.SignOut();
                    FormsAuthentication.RedirectToLoginPage();
                    return false;
                }
            }
            catch(Exception e)
            {
                FormsAuthentication.SignOut();
                FormsAuthentication.RedirectToLoginPage();
                return false;
            }

            return true;
        }
        #endregion

        #region JsonEncode
        public static string JsonEncode(object obj)
        {
            var serializer = new JavaScriptSerializer();
            return serializer.Serialize(obj);
        }
        public static T JsonDecode<T>(string text)
        {
            return new JavaScriptSerializer().Deserialize<T>(text);
        }
        public static object JsonDecode(string text, Type targetType)
        {
           return new JavaScriptSerializer().Deserialize(text, targetType);
        }
        #endregion

        #region
        public static string ConvertTextToHTML(string text)
        {
            if (string.IsNullOrEmpty(text))
                return string.Empty;

            return text.Replace("\r\n", "<br/>");
        }
        #endregion


        #region Get Or Set GridViewCellValue
        public static TData GetGridViewCellValue<TControl, TData>(GridView gridView, int row, string controlName, Expression<Func<TControl, string>> controlValue, TData defaultValue = default(TData))
            where TControl : Control
        {
            try
            {
                var control = gridView.Rows[row].FindControl(controlName) as TControl;
                var stringValue = controlValue.Compile()(control);
                return ConvertHelper.ChangeType<TData>(stringValue);
            }
            catch (Exception e)
            {
            }
            return defaultValue;
        }
        public static void SetGridViewCellValue<TControl>(GridViewRow gridViewRow, string controlName, Expression<Func<TControl, string>> controlValuePropertyExpression, object value)
            where TControl : Control
        {
            var control = gridViewRow.FindControl(controlName) as TControl;
            var valueString = value != null ? value.ToString() : "";
            ((controlValuePropertyExpression.Body as MemberExpression).Member as PropertyInfo).SetValue(control, valueString, null);
        }
        #endregion
    }
}